package cn.itcast.emp;

import cn.itcast.utils.JDBCutils;

import java.sql.*;
import java.util.Scanner;

/**
 * 版权：卓灿教育集团 版权所有
 * author :赵航
 *
 * @e-mail: zhaohangcn@gmail.com
 * time：2021/2/16
 * describe ：
 */
public class Login {

    public boolean isLogin(String name, String password) {
        if (name == null || name.isEmpty() || password == null || password.isEmpty()) {
            return false;
        }

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            //获取数据库的连接
            connection = JDBCutils.getConnection();
            String sql = "select * from user where username = '" + name + "' and passworld = '" + password + "'";

            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            return resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCutils.colse(resultSet,statement,connection);
        }
        return false;
    }

    public boolean isLogin2(String name, String password) {
        if (name == null || name.isEmpty() || password == null || password.isEmpty()) {
            return false;
        }

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //获取数据库的连接
            connection = JDBCutils.getConnection();
            String sql = "select * from user where username = ? and passworld = ?";

            statement = connection.prepareStatement(sql);
            //给?赋值
            statement.setString(1,name);
            statement.setString(2,password);

            resultSet = statement.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCutils.colse(resultSet,statement,connection);
        }
        return false;
    }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = scanner.nextLine();
        System.out.println("请输入密码");
        String pad = scanner.nextLine();
        boolean login = new Login().isLogin2(name, pad);
        if (login){
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }
    }
}
